package com.ekuaitu.kuaitu.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.ekuaitu.kuaitu.R;
import com.ekuaitu.kuaitu.adapter.MyInvoiceHistoryLvAdapter;
import com.ekuaitu.kuaitu.api.ApiStore;
import com.ekuaitu.kuaitu.api.UrlConstant;
import com.ekuaitu.kuaitu.base.BaseActivity;
import com.ekuaitu.kuaitu.bean.MyInvoiceHistoryBean;
import com.ekuaitu.kuaitu.utils.AppManager;
import com.ekuaitu.kuaitu.utils.MyApplication;
import com.ekuaitu.kuaitu.utils.SharedPreferencesUtils;
import com.ekuaitu.kuaitu.utils.StatusBarUtil;
import com.ekuaitu.kuaitu.utils.Toast.ToastCompat;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyInvoiceHistoryActivity extends BaseActivity {


    @BindView(R.id.return_myInvoice_history)
    ImageView returnMyInvoiceHistory;
    @BindView(R.id.toolbar_myInvoice_history)
    AutoRelativeLayout toolbarMyInvoiceHistory;
    @BindView(R.id.invoice_lv_history)
    ListView invoiceLvHistory;
    @BindView(R.id.invoice_pb_history)
    ProgressBar invoicePbHistory;
    @BindView(R.id.activity_my_invoice_history)
    AutoRelativeLayout activityMyInvoiceHistory;
    @BindView(R.id.layout_noInvoice)
    RelativeLayout layoutNoInvoice;
    private Context mContext = this;
    private List<MyInvoiceHistoryBean.AttachmentBean.InvoiceApplyModelsBean> mlist = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_invoice_history;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimaryWhite), 0,true);
        getInvoiceHistory();

        invoiceLvHistory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(MyInvoiceHistoryActivity.this, MyInvoiceDetailActivity.class);
                intent.putExtra("invoiceId", mlist.get(i).getId());
                startActivity(intent);
            }
        });
        invoiceLvHistory.setEmptyView(layoutNoInvoice);
    }

    private void getInvoiceHistory() {
        invoicePbHistory.setVisibility(View.VISIBLE);
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).getInvoiceHistory(MyApplication.getInstance().getRequestFieldMap()).enqueue(new Callback<MyInvoiceHistoryBean>() {
            @Override
            public void onResponse(Call<MyInvoiceHistoryBean> call, Response<MyInvoiceHistoryBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        invoicePbHistory.setVisibility(View.GONE);
                        mlist = response.body().getAttachment().getInvoiceApplyModels();
                        MyInvoiceHistoryLvAdapter adapter = new MyInvoiceHistoryLvAdapter(mlist, mContext);
                        invoiceLvHistory.setAdapter(adapter);
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }

                }
            }

            @Override
            public void onFailure(Call<MyInvoiceHistoryBean> call, Throwable t) {
                invoicePbHistory.setVisibility(View.GONE);
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
            }
        });
    }


    @OnClick(R.id.return_myInvoice_history)
    public void onClick() {
        finish();
    }
}
